/**
 * 部门管理初始化
 */
var Consultant = {
    id: "ConsultantTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Consultant.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '病历号', field: 'medicalRecordNumber', align: 'center', valign: 'middle', sortable: true, width: '4%',},
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                return "<a href='javascript:;' onclick='Consultant.consultantToDetail(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
            },
        },
        {title: '电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true},
        {title: '欠费', field: 'accountarrears', align: 'center', valign: 'middle', sortable: true,
            formatter:function (value, row, index) {
                return "<span style='color: red;'>"+row.accountarrears+"</span>"
            }
        },
        // {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true},
        // {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true},
        // {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true},
        {title: '看诊状态', field: 'diagnosisStatus', align: 'center', valign: 'middle', sortable: true},
        {
            title: '成交状态', field: 'dealStatus', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == "已成交") {
                    return "<a href='javascript:;' onclick='Consultant.opendealStatus(" + '"' + row.reservationsId + '"' + ")'>[已成交]</a>"
                } else {
                    return "未成交";
                }
            }
        },
        {title: 'TC', field: 'fieldTc', align: 'center', valign: 'middle', sortable: true},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true},
        {
            title: '客户详情', field: 'id', align: 'center', valign: 'middle', sortable: true, width: '6%',
            formatter: function (value, row, index) {
                return "<a href='javascript:;' onclick='Consultant.openscDetail(" + '"' + row.reservationsId + '"' + ")'>[市场录入]</a>" +
                    "<a href='javascript:;' onclick='Consultant.openqtDetail(" + '"' + row.id + '"' + ")'>[前台录入]</a>"
            }
        },
        {
            title: '看诊回访操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: '6%',
            formatter: function (value, row, index) {
                var cr=row.id+","+row.reservationsId;
                return   "<a href='javascript:;' onclick='Consultant.setDiagnosisRecord(" + '"' + cr + '"' + ")'>[看诊回访]</a>"
            }
        },
        {
            title: '开单操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: '8%',
            formatter: function (value, row, index) {
                var ids = row.reservationsId + "," + row.id;
                return "<a href='javascript:;' onclick='Consultant.openBile(" + '"' + ids + '"' + ")'>[开单记录]</a>" +
                    "<a href='javascript:;' onclick='Consultant.isOperation(" + '"' + row.id + '"' + ")'>[开单操作]</a>"
            }
        },
        {
            title: '是否回访', field: 'isReturnVisit', align: 'center', valign: 'middle', sortable: true, width: '4%',
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "";
                } else if (value == 1) {
                    return "<a href='javascript:;' onclick='Consultant.openRevisit(" + '"' + row.reservationsId + '"' + ")'>[是]</a>"
                } else if (value == 2) {
                    return "<a href='javascript:;' onclick='Consultant.openNoRevisit(" + '"' + row.reservationsId + '"' + ")'>[否]</a>"
                } else if (value == 3) {
                    return "已回访";
                }
            }
        },
        {title: '跟踪状态', field: 'followStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {title: '消费状态', field: 'consumptionStatus', align: 'center', valign: 'middle', sortable: true,
        },
        {title: '回访人', field: 'reservations', align: 'center', valign: 'middle', sortable: true},
        {title: '回访时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true},
        {
            title: '首次到店时间', field: 'firstVisitDate', align: 'center', valign: 'middle', sortable: true, width: '12%',
        },
        {
            title: '到店时间', field: 'arrivalTime', align: 'center', valign: 'middle', sortable: true, width: '6%',
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '实际余额', field: 'actualAccountBalance', align: 'center', valign: 'middle', sortable: true},
        {title: '赠送余额', field: 'giveAccountBalance', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

/**
 * 点击姓名进入详情
 */
Consultant.consultantToDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/consultantTwo/consultantToDetail/' +id
    });
    this.layerIndex = index;
};



/**
 * 打开查看预约客户详情
 */
Consultant.openqtDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/customer/customer_details/' + id
    });
    this.layerIndex = index;
};

/**
 * 点击预约客户
 */
Consultant.openscDetail = function (reservationsId) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/reservations_details/' + reservationsId
    });
    this.layerIndex = index;
};




/**
 * 检查是否选中
 */
Consultant.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Consultant.seItem = selected[0];
        return true;
    }
};


/**
 * 统计总人数
 */
Consultant.total = function () {
    //获取跟踪人数
    var param1 = $("#startArrivalTime").val();  //开始到店时间
    var param2 = $("#endArrivalTime").val();   //结束到店时间
    var param3 = $("#reach").val();  // 姓名  电话  微信 病历号
    var param4 = $("#consumptionStatus").val(); //消费状态
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/consultantTwo/total",
        data: {
            param1: param1, param2: param2, param3: param3,param4: param4
        },
        dataType: "json",
        success: function (data) {
            $("#total").html(data[0].countNum);//总人数
            $("#first").html(data[1].countNum);//新客首次
            $("#secondary").html(data[2].countNum);//新客二次
            $("#oldGuest").html(data[3].countNum);//老客
            $("#revisit").html(data[4].countNum);//复诊
        }
    });
}

Consultant.inquire = function () {

    Consultant.search()
}

/**
 * 查询预约客户列表
 */
Consultant.search = function () {
    Consultant.table.refresh({query: Consultant.formParams()});
};

Consultant.searchRevisit = function () {
    Consultant.table.refresh({query: Consultant.formParamss()});
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
Consultant.formParamss = function () {
    var queryData = {};
    queryData['reach'] = $("#reach").val();
    queryData['startArrivalTime'] = $("#startArrivalTime").val();
    queryData['endArrivalTime'] = $("#endArrivalTime").val();
    queryData['backVisit'] = "1";
    document.getElementById("backVisit").value="1";
    return queryData;
}

Consultant.resetSearch = function () {
    $("#startArrivalTime").val("");
    $("#endArrivalTime").val("");
    $("#consumptionStatus").val("");
    $("#reach").val("");
    document.getElementById("startArrivalTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endArrivalTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    Consultant.search();
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Consultant.formParams = function () {
    var queryData = {};

    queryData['reach'] = $("#reach").val();
    queryData['startArrivalTime'] = $("#startArrivalTime").val();
    queryData['endArrivalTime'] = $("#endArrivalTime").val();
    queryData['consumptionStatus'] = $("#consumptionStatus").val();

    queryData['backVisit'] = "";

    return queryData;
}
$(function () {

    document.getElementById("startArrivalTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endArrivalTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = Consultant.initColumn();
    var table = new BSTable(Consultant.id, "/consultant/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Consultant.formParams());
    Consultant.table = table.init();
    Consultant.search();
    Consultant.total();

    //采用定时器监听 时间内容是否变化
    var startArrivalTimeTwo = $("#startArrivalTime").val();
    var endArrivalTimeTwo = $("#endArrivalTime").val();

    setInterval(
        function () {//定时执行方法内容
            if ($("#startArrivalTime").val() != startArrivalTimeTwo || $("#endArrivalTime").val() != endArrivalTimeTwo) {//如果时间发生变化，就触发查询事件
                Consultant.search()
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                startArrivalTimeTwo = $("#startArrivalTime").val();
                endArrivalTimeTwo = $("#endArrivalTime").val();
            }

        }, 500);
});
